<?php

declare(strict_types=1);
/**
 * This file is part of MoChat.
 * @link     https://mo.chat
 * @document https://mochat.wiki
 * @contact  group@mo.chat
 * @license  https://github.com/mochat-cloud/mochat/blob/master/LICENSE
 */
namespace MoChat\App\WorkContact\Action\Workbench\Tag;

use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\Middleware;
use Hyperf\HttpServer\Annotation\Middlewares;
use Hyperf\HttpServer\Annotation\RequestMapping;
use MoChat\App\Common\Middleware\WorkbenchAuthMiddleware;
use MoChat\App\WorkContact\Contract\WorkContactTagContract;
use MoChat\App\WorkContact\Contract\WorkContactTagGroupContract;
use MoChat\App\WorkContact\Contract\WorkContactTagPivotContract;
use MoChat\Framework\Action\AbstractAction;

/**
 * 所有标签.
 *
 * Class AllTag
 * @Controller
 */
class AllTag extends AbstractAction
{
    /**
     * @Inject
     * @var WorkContactTagPivotContract
     */
    protected $workContactTagPivot;

    /**
     * @var
     */
    protected $contactTag;

    /**
     * @Inject
     * @var WorkContactTagContract
     */
    private $contactTagService;

    /**
     * @Inject
     * @var WorkContactTagGroupContract
     */
    private $contactTagGroupService;

    /**
     * @Middlewares({
     *     @Middleware(WorkbenchAuthMiddleware::class)
     * })
     * @RequestMapping(path="/workbench/workContactTag/allTag", methods="GET")
     */
    public function handle(): array
    {
        $params['groupId'] = $this->request->input('groupId');
        $params['contactId'] = (int) $this->request->input('contactId', 0);
        $params['employeeId'] = user()['id'];

        $columns = [
            'id',
            'name',
        ];

        // 获取所选分组下的标签列表
        if (is_numeric($params['groupId'])) {
            $tagInfo = $this->contactTagService
                ->getWorkContactTagsByCorpIdGroupIds([user()['corpId']], [$params['groupId']], $columns);

            if (empty($tagInfo)) {
                return [];
            }
        } else {
            // 获取所有标签
//            $tagInfo = $this->contactTagService
//                ->getWorkContactTagsByCorpId(user()['corpIds'], $columns);

            if ($params['contactId'] > 0) {
                $contactTags = $this->workContactTagPivot->getWorkContactTagPivotsByOtherId($params['contactId'], $params['employeeId'], ['contact_tag_id']);
                $this->contactTag = array_column($contactTags, 'contactTagId');
            }
            // 标签分组
            $tagInfo = $this->contactTagGroupService
                ->getWorkContactTagGroupsByCorpId(user()['corpIds'], ['id', 'group_name']);
            foreach ($tagInfo as $k => $v) {
                // 分组下标签
                $tags = $this->contactTagService
                    ->getWorkContactTagsByCorpIdGroupIds(user()['corpIds'], [$v['id']], $columns);
                // 删除空分组
                if (empty($tags)) {
                    unset($tagInfo[$k]);
                    continue;
                }
                if (! empty($this->contactTag)) {
                    foreach ($tags as $key => $val) {
                        $tags[$key]['isSelect'] = 0;
                        if (in_array($val['id'], $this->contactTag, true)) {
                            $tags[$key]['isSelect'] = 1;
                        }
                    }
                }
                $tagInfo[$k]['tags'] = $tags;
            }
            if (empty($tagInfo)) {
                return [];
            }
        }

        return array_merge($tagInfo);
    }
}
